﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ledict.ConditionExtract.BLL;
using Ledict.ConditionExtract.Common;
using Ledict.ConditionExtract.Model;

namespace Ledict.ConditionExtract
{
    public partial class WB_AddWordBook : Form
    {
        public string publicWB_ID = "";
        public string publicWB_Name = "";
        public string publicHeadWordId = "";

        public WB_AddWordBook()
        {
            InitializeComponent();
        }

        static WB_AddWordBook _default;
        public static WB_AddWordBook Default
        {
            get
            {
                if (_default == null || _default.IsDisposed)
                {
                    _default = new WB_AddWordBook();
                }
                return _default;
            }
        }

        TreeDataBLL trbll = new TreeDataBLL();
        WordBankBLL wordbankbll = new WordBankBLL();
        NewIDBLL newid = new NewIDBLL();
        RegexParse.RegexParse getregex = new RegexParse.RegexParse();

        private List<TreeNode> NodeList = new List<TreeNode>();

        private void Dialog_AddWordBook_Load(object sender, EventArgs e)
        {
            if (publicWB_Name != "")
            {
                WB_Name.Text = publicWB_Name;
            }
                GetData("t_Case_HeadWords");

                #region 定位到某节点
                if (  publicHeadWordId != "" && publicHeadWordId != "all")
                {
                    IndivHeadWord.Checked = true;
                    TV_HeadWords.Enabled = true;//案由树可用
                    string[] HeadWordArr = publicHeadWordId.Split(',');
                    foreach (var item in HeadWordArr)
                    {
                        ToLocateNode(item);//定位 
                    }
                }
                else
                {
                    AllHeadWord.Checked = true;
                    TV_HeadWords.Enabled = false;//案由树可用
                }
                #endregion
        }
        #region 定位
        /// <summary>
        /// 定位
        /// </summary>
        /// <param name="id">节点</param>
        private void ToLocateNode(string id)
        {
            string[] NotePidArr = getregex.GetParseValue("CC1509171748560001", trbll.GetAllParentNode(id)).Split(',');//找节点所有父级，去掉多余逗号 ;
            for (int i = 0; i < NotePidArr.Length; i++)
            {
                if (i != NotePidArr.Length - 1)//不是最后一层节点
                {
                    FindNode(NotePidArr[i]).Expand();//展开节点
                }
                else //最后一层节点
                {
                    TreeNode node = FindNode(NotePidArr[i]);
                    node.Checked = true;
                }
            }

        }
        /// <summary>
        /// 查找节点
        /// </summary>
        /// <param name="Nodeid"> 节点</param>
        /// <returns>返回结果</returns>
        private TreeNode FindNode(string Nodeid)
        {
            foreach (TreeNode node in NodeList)//遍历现在界面上的所有节点
            {
                if (node.Tag.ToString() == Nodeid)
                {
                    return node;
                }
            }
            return null;

        }

        #endregion
        public void GetData(string Tname)
        {
            DataTable dt = trbll.GetTreeHeadWords("", Tname);// 第一层
            DataRow[] arrayDR = dt.Select();

            foreach (DataRow CaseTreeNodeRode in arrayDR)
            {
                TreeNode node = new TreeNode();

                node.Text = CaseTreeNodeRode["name"].ToString() ;
                node.Tag = CaseTreeNodeRode["id"];

                if (trbll.GetTreeHeadWords(node.Tag.ToString(), Tname).Rows.Count > 0)//第二层
                {
                    node.Nodes.Add(new TreeNode());
                }
                TV_HeadWords.Nodes.Add(node);
                NodeList.Add(node);
            }
        }
        public void GetTreeHeadWordsChild(TreeNode parentNode, string Tname)
        {
            DataTable dt = trbll.GetTreeHeadWords(parentNode.Tag.ToString(), Tname);// 第一层
            DataRow[] arrayDR = dt.Select();

            foreach (DataRow CaseTreeNodeRode in arrayDR)
            {
                TreeNode node = new TreeNode();

                node.Text = LdFormat.ObjectToString(CaseTreeNodeRode["name"]) ;
                node.Tag = CaseTreeNodeRode["id"];
                if (trbll.GetTreeHeadWords(node.Tag.ToString(), Tname).Rows.Count > 0)//第二层
                {
                    node.Nodes.Add(new TreeNode());
                }
                parentNode.Nodes.Add(node);
                NodeList.Add(node);
            }

        }

        private void TV_HeadWords_AfterExpand(object sender, TreeViewEventArgs e)
        {
            e.Node.Nodes.Clear();
            GetTreeHeadWordsChild(e.Node, "t_Case_HeadWords");
        }

        private void Btn_Save_Click(object sender, EventArgs e)
        {
            SYS_WordBank Model = new SYS_WordBank();
            Model.WB_Name = WB_Name.Text;
            if (AllHeadWord.Checked)
            {
                Model.HeadWordIds = "all";
                Model.HeadWordNames = "通用";
            }
            else
            {
                foreach (TreeNode node in NodeList)
                {
                    if (node.Checked)
                    {
                        if (!string.IsNullOrEmpty(Model.HeadWordIds))
                        {
                            Model.HeadWordIds += "," + node.Tag.ToString();
                            Model.HeadWordNames += "、" + node.Text.ToString();
                        }
                        else
                        {
                            Model.HeadWordIds += node.Tag.ToString();
                            Model.HeadWordNames += node.Text.ToString();
                        }
                    }
                }
            }
            if (!String.IsNullOrEmpty(Model.WB_Name) && !String.IsNullOrEmpty(Model.HeadWordIds))
            {
                if (publicWB_ID != "")//修改
                {
                    Model.WB_ID = publicWB_ID;
                    Model.WB_UpdateTime = DateTime.Now;
                    if (wordbankbll.Update(Model))
                    {
                        MessageBox.Show("保存成功!");
                    }
                    else
                    {
                        MessageBox.Show("保存失败!");
                    }
                }
                else //新加
                {
                    Model.WB_ID = newid.JudgeDoc_CreateID("SYS_WordBank"); ;
                    Model.WB_CreateTime = DateTime.Now;
                    if (wordbankbll.Add(Model))
                    {
                        MessageBox.Show("保存成功!");
                    }
                    else
                    {
                        MessageBox.Show("保存失败!");
                    }
                }

                WB_Manage_Base wordbookmanage = WB_Manage_Base.Default;
                wordbookmanage.ShowWordBank();
                this.Close();
            }
            else
            {
                MessageBox.Show("词库名称和案由不能为空!");
            }

        }

        private void Btn_Clear_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void IndivHeadWord_CheckedChanged(object sender, EventArgs e)
        {
            TV_HeadWords.Enabled = true;
        }

        private void AllHeadWord_CheckedChanged(object sender, EventArgs e)
        {
            TV_HeadWords.Enabled = false ;
        }

  

    

     


    }
}
